package com.ruantu.clothing.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruantu.clothing.entity.JsonResponseMessage;
import com.ruantu.clothing.entity.Role;
import com.ruantu.clothing.entity.User;
import com.ruantu.clothing.service.RoleService;

@Controller
@RequestMapping("/role")
public class RoleController {

    @Autowired
    private RoleService roleService;
    private static final Logger logger = LoggerFactory.getLogger(RoleController.class);

    /**
     * 添加用戶获得角色
     *
     * @param session
     * @return
     */
    @RequestMapping("/findAllRole")
    @ResponseBody
    public JsonResponseMessage findAllRole(HttpSession session) {

        JsonResponseMessage msg = new JsonResponseMessage();
        try {
            User user = (User) session.getAttribute("info");
            List<Role> list = roleService.findAllRole(user);
            msg.setCode(0);
            msg.setData(list);
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(1);

        }
        return msg;

    }

    /**
     * 维护角色用
     *
     * @param session
     * @return
     */
    @RequestMapping("/getAllRole")
    @ResponseBody
    public JsonResponseMessage getAllRole(HttpSession session) {
        JsonResponseMessage msg = new JsonResponseMessage();
        try {
            User user = (User) session.getAttribute("info");
            Map<String, Object> map = roleService.getAllRole(user);
            msg.setCode(0);
            msg.setData(map);
        } catch (Exception e) {
            msg.setCode(1);

        }
        return msg;

    }


    /**
     * 修改用戶获得 角色
     *
     * @param session
     * @return
     */
    @RequestMapping("/findAllRoleXiu")
    @ResponseBody
    public JsonResponseMessage findAllUserXiu(int userId, HttpSession session) {

        JsonResponseMessage msg = new JsonResponseMessage();
        Map<String, Object> map = new HashMap<String, Object>();
        try {
            User user = (User) session.getAttribute("info");
            user.setId(userId);
            map = roleService.findAllRoleXiu(user);
            msg.setCode(0);
            msg.setData(map);
        } catch (Exception e) {
            msg.setCode(1);

        }
        return msg;

    }

    @RequestMapping("/findOneRole")
    @ResponseBody
    public JsonResponseMessage findOneRole(String rolename, HttpSession session) {
        JsonResponseMessage msg = new JsonResponseMessage();
        User u = (User) session.getAttribute("info");
        try {
            Role role = roleService.findOneRole(rolename);
            System.out.println(role.getRolename());
            if (role != null) {
                msg.setCode(0);
                msg.setData(role);
                logger.info("获取单个角色成功，执行人：" + u.getUserName());
            } else {
                logger.info("获取单个角色失败，执行人：" + u.getUserName());
                msg.setCode(1);

            }
        } catch (Exception e) {
            msg.setCode(1);
            logger.error("获取单个角色失败，执行人：" + u.getUserName());
            e.printStackTrace();
        }

        return msg;

    }

    @RequestMapping("/findOneRole1")
    @ResponseBody
    public JsonResponseMessage findOneRole1(String note) {
        JsonResponseMessage msg = new JsonResponseMessage();
        try {
            Role role = roleService.findOneRole1(note);
            if (role != null) {

                msg.setCode(0);

            } else {
                msg.setCode(1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }


        return msg;

    }

    /***
     * 添加角色
     *
     * @param role
     * @return
     */
    @RequestMapping("/AddRole")
    @ResponseBody
    public JsonResponseMessage AddRole(Role role, HttpSession session, String rp) {
        User u = (User) session.getAttribute("info");
        JsonResponseMessage msg = new JsonResponseMessage();
        try {
            Map<String, Object> map = roleService.tianRole(role, u, rp);
            if (map != null) {
                msg.setCode(0);
                msg.setData(map);
                logger.info("添加角色成功，执行人：" + u.getUserName());
            } else {
                logger.info("添加角色失败，执行人：" + u.getUserName());
                msg.setCode(1);
            }

        } catch (Exception e) {
            msg.setCode(1);
            logger.error("添加角色失败，执行人：" + u.getUserName(), e.getStackTrace());
        }

        return msg;

    }

    /***
     * 更新角色
     * @param role
     * @param session
     * @return
     */
    @RequestMapping("/upRole")
    @ResponseBody
    public JsonResponseMessage upRole(Role role, HttpSession session, String rp) {
        User u = (User) session.getAttribute("info");
        JsonResponseMessage msg = new JsonResponseMessage();
        try {
            Map<String, Object> map = roleService.gengRole(role, u, rp);
            if (map != null) {
                msg.setCode(0);
                msg.setData(map);
                logger.info("修改角色成功，执行人：" + u.getUserName());

            } else {
                msg.setCode(1);
                logger.info("修改角色，执行人：" + u.getUserName());
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("修改角色失败，执行人：" + u.getUserName(), e.getStackTrace());
        }

        return msg;

    }

    /***
     * 删除角色
     * @param id
     * @return
     */
    @RequestMapping("/delRole")
    @ResponseBody
    public JsonResponseMessage delRole(int id, HttpSession session) {
        User u = (User) session.getAttribute("info");
        JsonResponseMessage msg = new JsonResponseMessage();
        try {
            Map<String, Object> map = roleService.shanRole(id, u);
            if (map != null) {
                msg.setCode(0);
                msg.setData(map);
                logger.info("删除角色成功，执行人：" + u.getUserName());
            } else {
                logger.info("删除角色失败，执行人：" + u.getUserName());
                msg.setCode(1);
            }
        } catch (Exception e) {
            msg.setCode(1);
            e.printStackTrace();
            logger.error("删除角色失败，执行人：" + u.getUserName(), e.getStackTrace());
        }
        return msg;

    }

}
